package org.example.cake.service.impl;

import org.example.cake.dto.BuyCarRequest;
import org.example.cake.entity.Cake;
import org.example.cake.service.CakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.example.cake.mapper.BuycarMapper;
import org.example.cake.entity.Buycar;
import org.example.cake.service.BuycarService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class BuycarServiceImpl implements BuycarService{
    @Autowired
    private CakeService cakeService;

    @Resource
    private BuycarMapper buycarMapper;

    @Override
    public int deleteByPrimaryKey(Integer buyid) {
        return buycarMapper.deleteByPrimaryKey(buyid);
    }

    @Override
    public int insert(Buycar record) {
        return buycarMapper.insert(record);
    }

    @Override
    public int insertSelective(Buycar record) {
        return buycarMapper.insertSelective(record);
    }

    @Override
    public Buycar selectByPrimaryKey(Integer buyid) {
        return buycarMapper.selectByPrimaryKey(buyid);
    }

    @Override
    public int updateByPrimaryKeySelective(Buycar record) {
        return buycarMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Buycar record) {
        return buycarMapper.updateByPrimaryKey(record);
    }

    @Override
    public Integer addBuyCarapi(Buycar buycar) {
        return buycarMapper.addBuyCarapi(buycar);
    }
    /**
     * 查询购物车以及结算信息
     * @Date 2025-07-31 谭丽娜
     * @param userid 用户id
     * @return
     */
    @Override
    public  List<BuyCarRequest> selectBuycarListapi(Integer userid) {
        List<Buycar> buycarList = buycarMapper.selectBuycarListapi(userid);
        List<BuyCarRequest> buyCarRequestList = new ArrayList<>();
        //计算出总共价格
        for (Buycar buycar : buycarList) {
            BuyCarRequest buyCarRequest = new BuyCarRequest();

            //根据购物车的蛋糕id查询蛋糕信息
            Cake cake = cakeService.getCakeById(buycar.getCakeid());
            if(buycar.getType()==null){
                continue;
            }
            if(buycar.getType()==0){
                buyCarRequest.setPrice(cake.getCakeprice());
            }else {
                buyCarRequest.setPrice(cake.getCakepricebig());
            }
            buyCarRequest.setImage(cake.getCakeurl());
            buyCarRequest.setChecked(true);
            buyCarRequest.setId(buycar.getBuyid());
            buyCarRequest.setName(cake.getCakename());
            buyCarRequest.setCount(buycar.getCakesum());
            buyCarRequestList.add(buyCarRequest);
            buyCarRequest.setStatusquan(cake.getStatusquan());
            buyCarRequest.setCakeid(cake.getCakeid());
            buyCarRequest.setType(buycar.getType());
        }
        return buyCarRequestList;
    }
    /**
     * 修改购物车数量
     * @param buycar
     * @Date 2025-07-31 谭丽娜
     * @return
     */
    @Override
    public Integer updateCountapi(Buycar buycar) {
        return buycarMapper.updateCountapi(buycar);
    }

    @Override
    public Integer deleteBuyapi(Integer buyid) {
        return buycarMapper.deleteBuyapi(buyid);
    }

    @Override
    public Integer deleteBuyAllapi(Integer userid) {
        return buycarMapper.deleteBuyAllapi(userid);
    }
    /**
     * 立即购买
     * @param buycar
     * @Date 2025-07-31 谭丽娜
     * @return
     */
    @Override
    public BuyCarRequest getcakeNow(Buycar buycar) {
        BuyCarRequest buyCarRequest = new BuyCarRequest();
        //查询蛋糕
        Cake cake = cakeService.getCakeById(buycar.getCakeid());
        if(buycar.getType()==0){
            buyCarRequest.setPrice(cake.getCakeprice());
        }else {
            buyCarRequest.setPrice(cake.getCakepricebig());
        }
        buyCarRequest.setImage(cake.getCakeurl());
        buyCarRequest.setName(cake.getCakename());
        buyCarRequest.setCount(buycar.getCakesum());
        buyCarRequest.setStatusquan(cake.getStatusquan());
        buyCarRequest.setCakeid(cake.getCakeid());
        buyCarRequest.setType(buycar.getType());
        return buyCarRequest;
    }


    @Override
    public List<Map<String, Object>> getBuycarDetails() {
        return buycarMapper.selectBuycarDetails();
    }

    @Override
    public List<Map<String, Object>> getBuycarDetailsBySearch(Integer page, Integer pageSize, String searchKey) {
        // 计算offset
        Integer offset = (page - 1) * pageSize;
        return buycarMapper.selectBuycarDetailsBySearch(searchKey, pageSize, offset);
    }

    @Override
    public Integer countBuycarDetailsBySearch(String searchKey) {
        return buycarMapper.countBuycarDetailsBySearch(searchKey);
    }

    @Override
    public List<Map<String, Object>> getBuycarDetailsByUserId(Integer userId) {
        if (userId == null || userId <= 0) {
            throw new IllegalArgumentException("用户ID不能为空且必须大于0");
        }
        return buycarMapper.selectBuycarDetailsByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getBuycarDetailsByCakeId(Integer cakeId) {
        if (cakeId == null || cakeId <= 0) {
            throw new IllegalArgumentException("蛋糕ID不能为空且必须大于0");
        }
        return buycarMapper.selectBuycarDetailsByCakeId(cakeId);
    }

    @Override
    public List<Map<String, Object>> getBuycarDetailsByUserAndCake(Integer userId, Integer cakeId) {
        if (userId == null || userId <= 0) {
            throw new IllegalArgumentException("用户ID不能为空且必须大于0");
        }
        if (cakeId == null || cakeId <= 0) {
            throw new IllegalArgumentException("蛋糕ID不能为空且必须大于0");
        }
        return buycarMapper.selectBuycarDetailsByUserAndCake(userId, cakeId);
    }

    @Override
    @Transactional
    public boolean addBuycar(Buycar buycar) {
        if (buycar == null) {
            throw new IllegalArgumentException("购物车信息不能为空");
        }
        if (buycar.getUserid() == null || buycar.getUserid() <= 0) {
            throw new IllegalArgumentException("用户ID不能为空且必须大于0");
        }
        if (buycar.getCakeid() == null || buycar.getCakeid() <= 0) {
            throw new IllegalArgumentException("蛋糕ID不能为空且必须大于0");
        }
        if (buycar.getCakesum() == null || buycar.getCakesum() <= 0) {
            throw new IllegalArgumentException("蛋糕数量不能为空且必须大于0");
        }
        if (buycar.getType() == null || (buycar.getType() != 0 && buycar.getType() != 1)) {
            throw new IllegalArgumentException("蛋糕尺寸类型必须为0(4寸)或1(6寸)");
        }

        // 检查是否已存在相同用户、蛋糕和尺寸的记录
        Buycar existing = buycarMapper.getBuycarByUserAndCakeAndType(
                buycar.getUserid(), buycar.getCakeid(), buycar.getType());

        if (existing != null) {
            // 如果已存在，则更新数量
            existing.setCakesum(existing.getCakesum() + buycar.getCakesum());
            return buycarMapper.updateBuycar(existing) > 0;
        } else {
            // 不存在则新增
            return buycarMapper.insertBuycar(buycar) > 0;
        }
    }

    @Override
    @Transactional
    public boolean updateBuycar(Buycar buycar) {
        if (buycar == null || buycar.getBuyid() == null || buycar.getBuyid() <= 0) {
            throw new IllegalArgumentException("购物车ID不能为空且必须大于0");
        }
        if (buycar.getCakesum() != null && buycar.getCakesum() <= 0) {
            throw new IllegalArgumentException("蛋糕数量必须大于0");
        }

        return buycarMapper.updateBuycar(buycar) > 0;
    }

    @Override
    @Transactional
    public boolean deleteBuycar(Integer buyId) {
        if (buyId == null || buyId <= 0) {
            throw new IllegalArgumentException("购物车ID不能为空且必须大于0");
        }
        return buycarMapper.deleteBuycar(buyId) > 0;
    }

    @Override
    @Transactional
    public int batchDeleteBuycar(List<Integer> buyIds) {
        if (buyIds == null || buyIds.isEmpty()) {
            throw new IllegalArgumentException("购物车ID列表不能为空");
        }
        return buycarMapper.batchDeleteBuycar(buyIds);
    }
    /**
     * 清楚购物车
     * @param buycarid 购物车id
     * @return
     */
    @Override
    public Integer delBuycar(int buycarid) {
        return buycarMapper.delBuycar(buycarid);
    }
    /**
     * 查看购物车是否存在此蛋糕
     * @param buycar
     * @return
     */
    @Override
    public Integer selectBuyCarifhas(Buycar buycar) {
        return buycarMapper.selectBuyCarifhas(buycar);
    }
    /**
     * 查询购物车蛋糕数量
     * @param buycar
     * @return
     */
    @Override
    public Integer selectcakecount(Buycar buycar) {
        return buycarMapper.selectcakecount(buycar);
    }
    /**
     * 修改购物车信息
     * @param buycar
     * @return
     */
    @Override
    public Integer updatebuycarCount(Buycar buycar) {
        return buycarMapper.updatebuycarCount(buycar);
    }
    /**
     * 查询购物车里购物总数有木有
     * @Date 2025-08-11 谭丽娜
     * @param cakeid
     * @return
     */
    @Override
    public Integer cakecount(Integer cakeid) {
        return buycarMapper.cakecount(cakeid);
    }
    /**
     * 根据蛋糕id修改购物车数量
     * @param buycar
     * @return
     */
    @Override
    public Integer updateCountCakeid(Buycar buycar) {
        return buycarMapper.updateCountCakeid(buycar);
    }

}
